package com.dcrzl.gazgmh.module.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dcrzl.gazgmh.common.entity.mh.MhContent;
import com.dcrzl.gazgmh.common.entity.mh.MhMenu;
import com.dcrzl.gazgmh.common.mapper.mh.MhContentMapper;
import com.dcrzl.gazgmh.common.service.impl.BaseServiceImpl;
import com.dcrzl.gazgmh.common.utils.PageInfo;
import com.dcrzl.gazgmh.common.utils.Result;
import com.dcrzl.gazgmh.common.utils.SysConstant;
import com.dcrzl.gazgmh.module.mh.service.MhContentService;
import com.dcrzl.gazgmh.module.mh.service.MhMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;

@Service("mhContentService")
public class MhContentServiceImpl extends BaseServiceImpl<MhContentMapper, MhContent> implements MhContentService {

    @Autowired
    private MhMenuService mhMenuService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    private LambdaQueryWrapper<MhContent> commonWrapper(MhContent mhContent) {
        LambdaQueryWrapper<MhContent> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(mhContent.getCategoryIdListStr())) {
            wrapper.in(MhContent::getCategoryId, Arrays.asList(mhContent.getCategoryIdListStr().split(",")));
        }
        wrapper.eq(Objects.nonNull(mhContent.getCategoryType()), MhContent::getCategoryType, mhContent.getCategoryType());
        wrapper.like(StringUtils.hasText(mhContent.getTitle()), MhContent::getTitle, mhContent.getTitle());
        wrapper.eq(Objects.nonNull(mhContent.getCategoryId()), MhContent::getCategoryId, mhContent.getCategoryId());
        return wrapper;
    }

    @Override
    public Result<MhContent> selectOne(MhContent mhContent) {
        LambdaQueryWrapper<MhContent> wrapper = this.commonWrapper(mhContent);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<MhContent>> selectList(MhContent mhContent) {
        LambdaQueryWrapper<MhContent> wrapper = this.commonWrapper(mhContent);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<MhContent> selectPage(MhContent mhContent) {
        LambdaQueryWrapper<MhContent> wrapper = this.commonWrapper(mhContent);
        wrapper.orderByDesc(MhContent::getCreateTime).orderByAsc(MhContent::getId);
        return super.selectPage(mhContent, wrapper);
    }

    @Override
    public PageInfo<MhContent> selectUPage(MhContent mhContent) {
        PageInfo<MhContent> mhContentPageInfo = this.selectPage(mhContent);
        List<MhContent> list = mhContentPageInfo.getList();
        Set<Long> categoryIdList = new HashSet<>();
        list.forEach(mhContent1 -> {
            Long categoryId = mhContent1.getCategoryId();
            categoryIdList.add(categoryId);
        });
        if (!CollectionUtils.isEmpty(categoryIdList)) {
            Result<List<MhMenu>> listResult = mhMenuService.selectList(new MhMenu().setIdList(categoryIdList));
            if (listResult.isSuccess()) {
                List<MhMenu> mhMenuList = listResult.getData();
                Map<Long, MhMenu> map = new HashMap<>();
                mhMenuList.forEach(mhMenu -> {
                    map.put(mhMenu.getId(), mhMenu);
                });
                list.forEach(mhContent1 -> {
                    mhContent1.setMhMenu(map.get(mhContent1.getCategoryId()));
                });
            }
        }

        return mhContentPageInfo;
    }

    /**
     * 更新浏览次数
     * @param mhContent
     * @return
     */
    @Override
    public Result<Integer> updateCanNumById(MhContent mhContent) {
        String key = "contentId:" + mhContent.getId();
        Object o = redisTemplate.opsForValue().get(key);
        if(Objects.isNull(o)){
            Result<MhContent> mhContentResult = this.selectById(mhContent.getId());
            if(mhContentResult.isSuccess()){
                Long canNum = mhContentResult.getData().getCanNum();
                if(Objects.isNull(canNum)){
                    canNum = 0L;
                }
                redisTemplate.opsForValue().setIfAbsent(key,canNum);
            }
        }
        Long increment = redisTemplate.opsForValue().increment(key);
        mhContent.setCanNum(increment);
        return this.updateById(mhContent);
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        MhContent mhContent = new MhContent().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(mhContent);
    }

}